//
//  KRNavigationController.m
//  cookBook
//
//  Created by kuroken on 16/1/9.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRNavigationController.h"
#import "KRConst.h"
#import <SVProgressHUD.h>

@interface KRNavigationController () <UINavigationControllerDelegate>
/** 是否可执行push或pop */
@property (nonatomic, assign, getter=shouldPushOrPop) BOOL pushOrPop;
@end

@implementation KRNavigationController

+ (void)initialize
{
    // 统一设置顶部导航栏标题样式
    UINavigationBar *navBar = [UINavigationBar appearance];
    NSMutableDictionary *navBarAttrs = [NSMutableDictionary dictionary];
    navBarAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    navBarAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    [navBar setTitleTextAttributes:navBarAttrs];

    // 设置整个项目所有item的主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionary];
    itemAttrs[NSForegroundColorAttributeName] = KRColor(111, 143, 18);
    itemAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
    
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
    self.pushOrPop = YES;
    if (self = [super initWithRootViewController:rootViewController]) {
        self.delegate = self;
    }
    return self;
}



- (void)viewWillAppear:(BOOL)animated
{
    if (self.childViewControllers.count == 1) {
#warning (navigationBar clearColor)使顶部导航栏背景透明而不隐藏的两行代码
//        [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsCompact];
//        self.navigationBar.layer.masksToBounds = YES;

        // 隐藏顶部导航栏
        self.navigationBar.hidden = YES;
    }
    [super viewWillAppear:animated];

}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    NSLog(@"dealloc");
}

/*
 * 隐藏navigationBar底部黑线
 */
- (void)hidesNavigationBarBottomLine
{
    // 遍历navigationBar，找出底部黑线并隐藏
    NSArray *subviews = [self.navigationBar getSubviews];
    for (UIView *subview in subviews) {
        if ([subview isKindOfClass:[UIImageView class]] && subview.frame.size.height < 1.0) {
            [subview removeFromSuperview];
        }
    }
}

/**
 *  重写这个方法的目的 : 能够拦截所有push进来的控制器
 *
 *  @param viewController 即将push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (!self.shouldPushOrPop) return;
    self.pushOrPop = NO;

    // 隐藏navigationBar底部黑线
    [self hidesNavigationBarBottomLine];

    if (self.childViewControllers.count > 0) {
        // 隐藏底部tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        // 显示顶部导航栏
        self.navigationBar.hidden = NO;

        if (animated) {  // 如果是通过动画push进来的
            /* 设置返回按钮样式 */
            UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            // 设置按钮标题
            NSString *title = [self.childViewControllers lastObject].title;
            [backBtn setTitle: title ? title : @"返回" forState:UIControlStateNormal];
            // 设置按钮颜色
            [backBtn setTitleColor:KRColor(111, 143, 18) forState:UIControlStateNormal];
            // 设置按钮图片
            [backBtn setImage:[UIImage imageNamed:@"ico_back_arrow"] forState:UIControlStateNormal];
            [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
            // 按钮内部水平居左
            backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            // 设置按钮大小
            [backBtn setFrame:CGRectMake(0, 0, 100, 24)];
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            backBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
            viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        }        
    }
    [super pushViewController:viewController animated:animated];
}

/**
 *  拦截所有pop操作
 */
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if (!self.shouldPushOrPop) return nil;
    self.pushOrPop = NO;
    if (self.childViewControllers.count == 2) {
        // 隐藏顶部导航栏
        self.navigationBar.hidden = YES;
    }
    // 移除指示器
    [SVProgressHUD dismiss];
    
    return [super popViewControllerAnimated:animated];

}

/**
 *  动画执行完成调用这个方法
 */
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
#warning iOS7 Error Solved(上一个的push或pop操作的动画未执行完就马上执行下个push或pop操作而导致的bug)
    self.pushOrPop = YES;
}


- (void)back
{
    [self popViewControllerAnimated:YES];
}
@end
